package com.gson.framework.tool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.json.Json;

public class BuildJavascript {

	public static void main(String[] args) {
		JavascriptUtils cj = new JavascriptUtils("../web/jssrc/", null);
		List<String> ls = cj.getListFiles("F:/workspace/xpro/main/web/jssrc/", "js", true);
		Map<String, String> config = new HashMap<String, String>();
		for (String path : ls) {
			File file = new File(path);
			FileReader fi;
			BufferedReader br;
			try {
				fi = new FileReader(file);
				br = new BufferedReader(fi);
				while(br.ready()){
					String line = br.readLine();
					if(line.indexOf("package") == -1){
						System.out.println(path+"未定义package");
						break;
					}
					int start = line.indexOf("\"");
					int end = line.lastIndexOf("\"");
					config.put(line.substring(start+1,end), path.substring(path.indexOf("/jssrc")));
					break;
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		 try {
			FileWriter fw = new FileWriter("F:/workspace/xpro/main/web/jslib/package.js");
			BufferedWriter bw = new BufferedWriter(fw);
			 bw.write("gson.packages="+Json.toJson(config)+";");
			 bw.flush();    //刷新该流的缓冲
	         bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
